package com.bigzero.workflow.modules.system.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bigzero.workflow.common.exception.WorkflowException;
import com.bigzero.workflow.modules.system.dao.MenuDao;
import com.bigzero.workflow.modules.system.dao.RoleDao;
import com.bigzero.workflow.modules.system.dao.RoleMenuDao;
import com.bigzero.workflow.modules.system.dto.RoleMenuDto;
import com.bigzero.workflow.modules.system.entity.MenuEntity;
import com.bigzero.workflow.modules.system.entity.RoleEntity;
import com.bigzero.workflow.modules.system.entity.RoleMenuEntity;
import com.bigzero.workflow.modules.system.service.RoleMenuService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.UUID;

/**
 * 角色关联菜单实现类
 * @Author: fanxinxiong
 * @Date: 2025-02-11 14:48:00
 */
@Service
public class RoleMenuServiceImpl implements RoleMenuService{

    @Resource
    private RoleMenuDao roleMenuDao;
    @Resource
    private RoleDao roleDao;
    @Resource
    private MenuDao menuDao;

    /**
     * 根据角色id查询菜单id集合
     * @param roleId 角色id
     * @return List<String>
     */
    public List<String> getMenuIdListByRoleId(String roleId){
        return roleMenuDao.getMenuIdListByRoleId(roleId);
    }

    /**
     * 保存角色关联菜单id集合
     * @param roleMenuDto
     * @return int
     */
    public int saveRoleMenuIdList(RoleMenuDto roleMenuDto){
        int result = 1;
        RoleEntity roleEntity = roleDao.selectById(roleMenuDto.getRoleId());
        if(roleEntity == null){
            throw new WorkflowException("失败:角色不存在");
        }

        //根据角色id删除角色菜单关系
        roleMenuDao.deleteByRoleId(roleEntity.getId());

        if(!CollectionUtils.isEmpty(roleMenuDto.getMenuIdList())){
            List<RoleMenuEntity> list = new ArrayList<RoleMenuEntity>();
            for(String menuId : roleMenuDto.getMenuIdList()){
                RoleMenuEntity entity = new RoleMenuEntity();
                entity.setId(UUID.randomUUID().toString());
                entity.setRoleId(roleEntity.getId());
                entity.setMenuId(menuId);
                list.add(entity);
            }
            roleMenuDao.batchInsert(list);
        }
        return result;
    }

    /**
     * 根据角色id查询菜单id集合(层级)
     * @param roleId 角色id
     * @return JSONObject
     */
    public JSONObject getLevelMenuIdListByRoleId(String roleId){
        JSONObject respJson = new JSONObject();
        List<String> parentIdList = new ArrayList<>();//父级菜单id集合
        List<String> lastLevelMenuIdList = new ArrayList<>();//末级菜单id集合
        List<String> nonLastLevelMenuIdList = new ArrayList<>();//非末级菜单id集合
        //菜单id集合
        List<String> menuIdList = roleMenuDao.getMenuIdListByRoleId(roleId);
        menuIdList = Optional.ofNullable(menuIdList).orElse(new ArrayList<>());

        LambdaQueryWrapper<MenuEntity> lqw = new QueryWrapper<MenuEntity>().lambda();
        lqw.select(MenuEntity :: getParentId);
        lqw.groupBy(MenuEntity :: getParentId);

        List<Object> objectIdList = menuDao.selectObjs(lqw);
        objectIdList = Optional.ofNullable(objectIdList).orElse(new ArrayList<>());
        objectIdList.forEach(i->{
            parentIdList.add(String.valueOf(i));
        });

        for(String menuId : menuIdList){
            if(parentIdList.contains(menuId)){
                nonLastLevelMenuIdList.add(menuId);
            }else{
                lastLevelMenuIdList.add(menuId);
            }
        }

        respJson.put("lastLevelMenuIdList", lastLevelMenuIdList);
        respJson.put("nonLastLevelMenuIdList", nonLastLevelMenuIdList);
        return respJson;
    }

}
